package jwdt.xadmin.custom.action;

import jwdt.common.action.BaseAction;
import jwdt.xadmin.custom.bean.TContacts;
import jwdt.xadmin.custom.bean.TCustom;
import jwdt.xadmin.custom.service.CustomManageService;
import jwdt.xadmin.system.bean.TUser;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.StringUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;

@Controller("/xadmin/custom")
public class CustomManageAction extends BaseAction {
	
	private CustomManageService customManageService = (CustomManageService)app.getBean("customManageService");
	
	@RequestMapping("/mylistCustom")
	public Page doMyListCustom(Form f, Module m) throws Exception {
		TCustom tc = (TCustom)f.copy2Class(TCustom.class);
		TUser userInfo = (TUser) f.getSession("userInfo");
		PageUtil pu = (PageUtil)f.copy2Class(PageUtil.class);
		return m.getWritePage(f, customManageService.doList(pu, tc, userInfo.getUserid()).toJson());
	}
	
	@RequestMapping("/listCustom")
	public Page doListCustom(Form f, Module m) throws Exception {
		PageUtil pu = (PageUtil)f.copy2Class(PageUtil.class);
		TCustom tc = (TCustom)f.copy2Class(TCustom.class);
		return m.getWritePage(f, customManageService.doList(pu, tc).toJson());
	}
	
	@RequestMapping("/addCustom")
	public Page doAddact(Form f, Module m) throws Exception {
		TCustom obj = (TCustom)f.copy2Class(TCustom.class);
		String contact = f.getString("contact");
		return m.getWritePage(f, customManageService.doAddact(obj,contact));
	}
	
	@RequestMapping("/editCustom")
	public Page doEditact(Form f, Module m) throws Exception {
		TCustom obj = (TCustom)f.copy2Class(TCustom.class);
		String contact = f.getString("contact");
		Integer id = f.getInt("id");
		return m.getWritePage(f, customManageService.doEditact(obj,id,contact));
	}
	
	@RequestMapping("/listCustomTypeForCombox")
	public Page doListCustomTypeForCombox(Form f, Module m) throws Exception {
		//生成json字符串
    	return m.getJsonPage(f, Jdb.get().query("SELECT mxid, mxmc FROM jwdt_zd_flmx WHERE flid=2 ORDER BY px"));
	}
	
	@RequestMapping("/listContacts")
	public Page doListContacts(Form f, Module m) throws Exception {
		PageUtil pu = (PageUtil)f.copy2Class(PageUtil.class);
		pu.setRows(100);
		String customId = f.getString("customId");
		if(!StringUtil.nullOrBlank(customId)){
			pu.doList("SELECT tc.id,tc.name,tc.duty,tc.email,tc.tel,tc.phone,tc.dt FROM t_contacts tc LEFT JOIN t_custom_contacts  cc ON tc.id=cc.contacts_id WHERE cc.custom_id = "+customId);
		}
		return m.getWritePage(f, pu.toJson());
	}
	
	@RequestMapping("/customWindow")
	public Page doLoad(Form f, Module m) throws Exception {
		int id = f.getInt("id");
		f.add("obj", customManageService.doLoad(id));
		return m.defaultPage();
	}
	
	@RequestMapping("/delContacts")
	public Page doDelContacts(Form f, Module m) throws Exception {
		int id = f.getInt("id");
		Jdb.get().del(TContacts.class, "id="+id);
		Jdb.get().execute("delete from t_custom_contacts where contacts_id = "+id);
		return m.getWritePage(f, "");
	}
	
	@RequestMapping("/delCustom")
	public Page doDel(Form f, Module m) throws Exception {
		int id = f.getInt("id");
		return m.getWritePage(f, customManageService.doDelact(id));
	}
}
